package com.example.t1;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.t1.activity.Activity0;
import com.example.t1.activity.Activity1;
import com.example.t1.activity.Activity2;
import com.example.t1.activity.Activity3;
import com.example.t1.activity.Activity4;
import com.example.t1.activity.Activity5;
import com.example.t1.activity.Activity7;
import com.example.t1.activity.Activity8;
import com.example.t1.activity.Activity9;
import com.example.t1.activity.ImagContrlActivity;
import com.example.t1.view.CommonListView;
import com.example.t1.view.CommonListView.GetViewInterface;

public class MainActivity extends BaseViewActivity implements OnClickListener,
	OnItemClickListener, GetViewInterface {
    LinearLayout layout1 = null;
    LinearLayout layout2 = null, layout3 = null;
    Button button1, button2, button3;

    CommonListView<String> listView;
    List<String> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	datas = new ArrayList<String>();
	datas.add("反射机制实例");
	datas.add("获取屏幕分辨率");
	datas.add("异步任务");
	datas.add("缩放");
	datas.add("图像剪裁");
	datas.add("TabActivity的替代品FragmentTabHost");
	datas.add("线程池ThreadPoolExecutor使用简介");
	datas.add("socket请求以及消息推送");
	datas.add("zeroMQ");

	listView = (CommonListView<String>) findViewById(R.id.commonListView1);
	listView.setDatas(datas);
	listView.getView(this);
	listView.setOnItemClickListener(this);

	button1 = (Button) findViewById(R.id.button1);

	button2 = (Button) findViewById(R.id.button2);
	button3 = (Button) findViewById(R.id.button3);
	button1.setOnClickListener(this);
	button2.setOnClickListener(this);
	button3.setOnClickListener(this);
	layout1 = (LinearLayout) findViewById(R.id.linear1);
	layout2 = (LinearLayout) findViewById(R.id.linear2);
	layout3 = (LinearLayout) findViewById(R.id.linear3);
	layout1.getViewTreeObserver().addOnGlobalLayoutListener(
		new OnGlobalLayoutListener() {

		    @Override
		    public void onGlobalLayout() {
			Log.d("tag", "layout1");
		    }
		});
	layout2.getViewTreeObserver().addOnGlobalLayoutListener(
		new OnGlobalLayoutListener() {

		    @Override
		    public void onGlobalLayout() {
			Log.d("tag", "layout2");
		    }
		});
	layout3.getViewTreeObserver().addOnGlobalLayoutListener(
		new OnGlobalLayoutListener() {

		    @Override
		    public void onGlobalLayout() {
			Log.d("tag", "layout3");
		    }
		});

    }

    @Override
    public void onClick(View arg0) {
	Intent intent;
	switch (arg0.getId()) {
	case R.id.button1:
	    TextView textView = new TextView(MainActivity.this);
	    textView.setText("1");
	    layout1.addView(textView);

	    break;
	case R.id.button2:
	    intent = new Intent(MainActivity.this, Activity1.class);
	    startActivityForResult(intent, 1000);
	    break;
	case R.id.button3:
	    intent = new Intent(MainActivity.this, Activity2.class);
	    startActivityForResult(intent, 1000);
	    break;
	default:
	    break;
	}
    }

    class ViewHolder {
	TextView name;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
	    long id) {
	Intent intent = null;
	switch (position) {
	// 反射机制
	case 0:
	    intent = new Intent(this, Activity0.class);
	    break;
	// 获取手机屏幕分辨率和
	case 1:
	    intent = new Intent(this, Activity4.class);
	    break;
	// 异步任务
	case 2:
	    intent = new Intent(this, Activity3.class);
	    break;
	// 缩放
	case 3:
	    intent = new Intent(this, ImagContrlActivity.class);
	    break;
	// 图像剪裁
	case 4:
	    intent = new Intent(this, CutActivity.class);
	    intent.putExtra("style", 1);
	    break;
	// TabActivity的替代品FragmentTabHost
	case 5:
	    intent = new Intent(this, Activity5.class);
	    break;
	// 线程池ThreadPoolExecutor使用简介
	case 6:
	    intent = new Intent(this, Activity7.class);
	    break;
	case 7:
	    intent = new Intent(this, Activity8.class);
	    break;
	case 8:
	    intent = new Intent(this, Activity9.class);
	    break;
	default:
	    break;
	}
	startActivity(intent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	ViewHolder holder = null;
	if (convertView == null) {
	    holder = new ViewHolder();
	    convertView = inflater.inflate(android.R.layout.simple_list_item_2,
		    null);

	    holder.name = (TextView) convertView
		    .findViewById(android.R.id.text1);
	    convertView.setTag(holder);
	} else {
	    holder = (ViewHolder) convertView.getTag();
	}
	holder.name.setText(datas.get(position));
	return convertView;
    }
}
